package com.licheedev.someext

import android.content.res.ColorStateList
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.text.Html
import android.text.Spanned
import android.util.TypedValue
import android.widget.TextView
import androidx.annotation.DimenRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.licheedev.context.AppProvider


/** 获取文本资源 */
fun Int.toStringRes(): String {
    return AppProvider.application.getString(this)
}

/** 获取文本资源 */
fun Int.toStringRes(vararg formatArgs: Any): String {
    return AppProvider.application.getString(this, *formatArgs)
}

/** 获取颜色资源 */
fun Int.toColorRes(): Int {
    return ContextCompat.getColor(AppProvider.application, this)
}

/** 获取颜色Selector资源 */
fun Int.toColorStateListRes(): ColorStateList? {
    return ContextCompat.getColorStateList(AppProvider.application, this)
}

/** 获取尺寸资源，像素  */
fun Int.toPixelDimenRes(): Float {
    return AppProvider.application.resources.getDimension(this)
}

/** 获取带html格式的字符串资源 */
fun Int.toHtmlStringRes(): Spanned {
    return Html.fromHtml(this.toStringRes())
}

/** 获取带html格式的字符串资源 */
fun Int.toHtmlStringRes(vararg formatArgs: Any): Spanned {
    return Html.fromHtml(AppProvider.application.getString(this, *formatArgs))
}

/** 获取带html格式的字符串资源 */
fun Int.toHtmlStringRes(resources: Resources): Spanned {
    return Html.fromHtml(resources.getString(this))
}


/** 获取带html格式的字符串资源 */
fun Int.toHtmlStringRes(resources: Resources, vararg formatArgs: Any): Spanned {
    return Html.fromHtml(resources.getString(this, *formatArgs))
}

/** 设置TextView字体大小 */
fun TextView.setTextSize(@DimenRes id: Int) {
    this.setTextSize(TypedValue.COMPLEX_UNIT_PX, this.resources.getDimension(id))
}

/** 设置带html格式的字符串资源 */
fun TextView.setHtmlStringRes(@StringRes stringRes: Int) {
    this.text = stringRes.toHtmlStringRes(this.resources)
}

/** 设置带html格式的字符串资源 */
fun TextView.setHtmlStringRes(@StringRes stringRes: Int, vararg formatArgs: Any) {
    this.text = stringRes.toHtmlStringRes(this.resources, *formatArgs)
}

/** 获取图片资源 */
fun Int.toDrawableRes(): Drawable? {
    return ContextCompat.getDrawable(AppProvider.application, this)
}

